package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced;


import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelProxyFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.AbstractHandleHttpReportServerProxyClientTransferResponseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelFlowEnum;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelProtocolType;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelProxyType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.proxy.ServerChannelProxyFlow;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
import org.wu.framework.core.utils.ObjectUtils;


@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleHttpReportServerProxyClientTransferResponseTypeAdvanced
        extends AbstractHandleHttpReportServerProxyClientTransferResponseTypeAdvanced<NettyProxyMsg> {
    private final ChannelProxyFlowAdapter channelProxyFlowAdapter;

    public ServerHandleHttpReportServerProxyClientTransferResponseTypeAdvanced(ChannelProxyFlowAdapter channelProxyFlowAdapter) {
        this.channelProxyFlowAdapter = channelProxyFlowAdapter;
    }

    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
        Channel channel = nettyChannelContext.channel();

        // 将返回数据
        Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
        String clientId = ChannelAttributeKeyUtils.getClientId(nextChannel);
        Integer visitorPort = ChannelAttributeKeyUtils.getVisitorPort(nextChannel);
        String targetClientId = ChannelAttributeKeyUtils.getTargetClientId(nextChannel);
        String targetIp = ChannelAttributeKeyUtils.getTargetIp(nextChannel);
        Integer targetPort = ChannelAttributeKeyUtils.getTargetPort(nextChannel);

        if (ObjectUtils.isNotEmpty(nextChannel)) {
            log.info("目标客户端返回数据通过服务端下发到原始通道");
            ByteBuf buf = channel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
            buf.writeBytes(nettyProxyMsg.getData());
            nextChannel.writeAndFlush(buf);

            // 记录出口数据
            ServerChannelProxyFlow serverChannelFlow = ServerChannelProxyFlow
                    .builder()
                    .channelFlowEnum(ChannelFlowEnum.OUT_FLOW)
                    .port(targetPort)
                    .ip(targetIp)
                    .clientId(targetClientId)
                    .channelProxyType(ChannelProxyType.SERVER_PROXY_CLIENT)
                    .channelProtocolType(ChannelProtocolType.HTTP)
                    .flow(nettyProxyMsg.getData().length)
                    .build();
            channelProxyFlowAdapter.asyncHandler(channel, serverChannelFlow);
        } else {
            log.error("无法将数据下发给原始客户端");
        }

    }

}
